package com.toe.recommend.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.business.service.BusinessInformationService;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.restful.StateEnum;
import com.toe.pojo.entity.business.BusinessInformation;
import com.toe.pojo.vo.recommend.RecommendBusinessVO;
import com.toe.recommend.service.RecommendBusinessService;
import com.toe.recommend.webapi.mapper.RecommendBusinessMapper;
import com.toe.recommend.webapi.pojo.LngLat;
import com.toe.recommend.webapi.util.LocationUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@DubboService
@Slf4j
public class RecommendBusinessServiceImpl extends ServiceImpl<RecommendBusinessMapper, BusinessInformation> implements RecommendBusinessService {

    @DubboReference
    BusinessInformationService businessInformationService;

    public List<RecommendBusinessVO> recommendBusiness(String location) {
//        List<String> list = LocationUtil.getAround(location);
//
//        if (list == null || list.isEmpty()) {
//            throw new ToeServiceException(
//                    StateEnum.ERR_SERVER_ERROR, "服务器忙,请稍后重试");
//        }
//
////        List<String> ids = new ArrayList<>();
//
//        System.out.println("==================前"+list.size());
//        list.forEach(System.out::println);
//        if (list.size() > 20) {
//            list = list.subList(0, 20);
//        }
//        System.out.println("==================后"+list.size());
//        list.forEach(System.out::println);
//
//        List<RecommendBusinessVO> recommendBusinessVOS =
//                businessInformationService.businessByIds(list);
//        System.out.println("===============继续");
//        recommendBusinessVOS.forEach(System.out::println);
//        System.out.println("==========完==========");
//        for (RecommendBusinessVO information : recommendBusinessVOS) {
//            LngLat start = new LngLat(location);
//            LngLat end = new LngLat(information.getLocation());
//            Double distance = LocationUtil.calculateLineDistance(start, end);
//            information.setDistance(distance);
//        }
//
//        return recommendBusinessVOS;
        List<BusinessInformation> businessList =
                businessInformationService.list();
        List<RecommendBusinessVO> resultList = new ArrayList<>();
        for (BusinessInformation business : businessList) {
            if (business.getLocation() != null) {
                RecommendBusinessVO businessVO = new RecommendBusinessVO();
                BeanUtils.copyProperties(business, businessVO);

                LngLat start = new LngLat(location);
                LngLat end = new LngLat(business.getLocation());
                Double distance = LocationUtil.calculateLineDistance(start, end);
                businessVO.setDistance(distance);
                resultList.add(businessVO);
            }
        }

        Object[] objects = resultList.toArray();
        RecommendBusinessVO[] result = new RecommendBusinessVO[objects.length];
        for (int i = 0; i < objects.length; i++) {
            result[i] = (RecommendBusinessVO) objects[i];
        }
        Arrays.sort(result, Comparator.comparing(RecommendBusinessVO::getDistance));

        return Arrays.asList(result).subList(0, 16);
//        return resultList;
    }

}
